/**  
 * <p>Title: s.java</p>  
 * <p>Description: TODO</p>  
 * <p>Copyright: Copyright (c) 2017</p>  
 * <p>Company: www.***.com</p>  
 *
 * @author wanjx 
 * @date 20192019年4月5日下午8:18:58
 * @version 1.0 
 */  
package com.wx.util;

import java.security.MessageDigest;  
import java.security.NoSuchAlgorithmException;  
import java.util.Random;

import org.apache.commons.codec.binary.Hex;  


/**
 * <p>Title: MD5Utils</p>  
 * <p>Description: 加密解密工具类</p>
 *
 * @author wanjx 
 * @date 2019年4月5日下午8:27:07
 */
public class MD5Utils {  

	/** 
	 * 普通MD5加密 01 
	 */  
	public static String getStrMD5(String inStr) {  
		// 获取MD5实例  
		MessageDigest md5 = null;  
		try {  
			md5 = MessageDigest.getInstance("MD5");  
		} catch (NoSuchAlgorithmException e) {  
			e.printStackTrace();  
			System.out.println(e.toString());  
			return "";  
		}  

		// 将加密字符串转换为字符数组  
		char[] charArray = inStr.toCharArray();  
		byte[] byteArray = new byte[charArray.length];  

		// 开始加密  
		for (int i = 0; i < charArray.length; i++)  
			byteArray[i] = (byte) charArray[i];  
		byte[] digest = md5.digest(byteArray);  
		StringBuilder sb = new StringBuilder();  
		for (int i = 0; i < digest.length; i++) {  
			int var = digest[i] & 0xff;  
			if (var < 16)  
				sb.append("0");  
			sb.append(Integer.toHexString(var));  
		}  
		return sb.toString();  
	}  

	/** 
	 * 普通MD5加密 02 
	 */  
	public static String getStrrMD5(String password) {  

		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };  
		try {  
			byte strTemp[] = password.getBytes("UTF-8");  
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");  
			mdTemp.update(strTemp);  
			byte md[] = mdTemp.digest();  
			int j = md.length;  
			char str[] = new char[j * 2];  
			int k = 0;  
			for (int i = 0; i < j; i++) {  
				byte byte0 = md[i];  
				str[k++] = hexDigits[byte0 >>> 4 & 15];  
				str[k++] = hexDigits[byte0 & 15];  
			}  

			return new String(str);  
		} catch (Exception e) {  
			return null;  
		}  
	}  

	/** 
	 * MD5双重解密 
	 */  
	public static String getconvertMD5(String inStr) {  
		char[] charArray = inStr.toCharArray();  
		for (int i = 0; i < charArray.length; i++) {  
			charArray[i] = (char) (charArray[i] ^ 't');  
		}  
		String str = String.valueOf(charArray);  
		return str;  
	}  

	/** 
	 * 使用Apache的Hex类实现Hex(16进制字符串和)和字节数组的互转 
	 */  
	@SuppressWarnings("unused")  
	private static String md5Hex(String str) {  
		try {  
			MessageDigest md = MessageDigest.getInstance("MD5");  
			byte[] digest = md.digest(str.getBytes());  
			return new String(new Hex().encode(digest));  
		} catch (Exception e) {  
			e.printStackTrace();  
			System.out.println(e.toString());  
			return "";  
		}  
	}  

	/** 
	 * 加盐MD5加密 
	 */  
	public static String getSaltMD5(String password,String salt) {  
		//加盐
		password = md5Hex(password + salt);  
		char[] cs = new char[48];  
		for (int i = 0; i < 48; i += 3) {  
			cs[i] = password.charAt(i / 3 * 2);  
			char c = salt.charAt(i / 3);  
			cs[i + 1] = c;
			cs[i + 2] = password.charAt(i / 3 * 2 + 1);  
		}  
		return String.valueOf(cs);  
	}  

	/** 
	 * 验证加盐后是否和原文一致 
	 */  
	public static boolean getSaltverifyMD5(String password,String salt,String md5str) {  
		char[] cs1 = new char[32];  
		char[] cs2 = new char[16];  
		for (int i = 0; i < 48; i += 3) {  
			cs1[i / 3 * 2] = md5str.charAt(i);  
			cs1[i / 3 * 2 + 1] = md5str.charAt(i + 2);  
			cs2[i / 3] = md5str.charAt(i + 1);  
		}  
		return md5Hex(password + salt).equals(String.valueOf(cs1));  
	}  

}  
